import requests
import re
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys






class Qksw:
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'}
    def __init__(self,code = 'gbk'):
        self.encoding = code
        self.s = requests.Session()
        self.s.headers.update(self.headers)


    def html(self,url):
        
        res = self.s.get(url) 
        res.encoding = self.encoding
        html = res.text

        return html


    def search(self,keyword = '大主宰'):
        chrome_options = Options()
        chrome_options.add_argument('--no-sandbox')#解决DevToolsActivePort文件不存在的报错
        chrome_options.add_argument('window-size=1920x3000') #指定浏览器分辨率
        chrome_options.add_argument('--disable-gpu') #谷歌文档提到需要加上这个属性来规避bug
        chrome_options.add_argument('--hide-scrollbars') #隐藏滚动条, 应对一些特殊页面
        chrome_options.add_argument('blink-settings=imagesEnabled=false') #不加载图片, 提升速度
        chrome_options.add_argument('--headless')
    
        browser = webdriver.Chrome(chrome_options = chrome_options)
        browser.get('https://www.qksw.com')
        browser.find_element_by_id("shuming").send_keys(keyword)
        browser.find_element_by_id("shuming").send_keys(Keys.ENTER)
        for handle in browser.window_handles:
            browser.switch_to_window(handle)
        html = browser.page_source

        browser.close()
        browser.quit()

        novel_info_regex = '<td class="odd"><a href="(.*?)">(.*?)</a></td>'
        novel_author_regex = '<td class="odd">(?!<a)(.*?)</td>'
        latest_chapter_info_regex = '<td class="even"><a href="(.*?)" target="_blank">(.*?)</a></td>'
        refresh_time_regex = '<td class="odd" align="center">(.*?)</td>'
        novel_status_regex = '<td class="even" align="center">(.*?)</td>'
        novel_words_regex = '<td class="even">(?!<a)(.*?)</td>'
        
        novel_info = re.findall(novel_info_regex,html)
        novel_author = re.findall(novel_author_regex,html)
        latest_chapter_info = re.findall(latest_chapter_info_regex,html)
        refresh_time = re.findall(refresh_time_regex,html)
        novel_status = re.findall(novel_status_regex,html)
        novel_words = re.findall(novel_words_regex,html)
        
        novel_a = {}
        if len(novel_info)>10:
            a = 10
        elif len(novel_info)<10:
            a = len(novel_info)
        for i in range(len(novel_info[:a])):
            novel_a[novel_info[i][1]] = {
                                            'novel_author':novel_author[i],
                                            'novel_author_url':None,
                                            'novel_url':novel_info[i][0],
                                            'latest_chapter':latest_chapter_info[i][1],
                                            'latest_chapter_url':latest_chapter_info[i][0],
                                            'novel_type':None,
                                            'refresh_time':refresh_time[i],
                                            'novel_status':novel_status[i],
                                            'novel_img':None
                                            }

        return novel_a




    def homepage(self,url = 'https://www.qksw.com'):
        html = self.html(url)

        novel_info_regex = '<dt><a href="(.*?)" title=".*?" target="_blank"><h1>(.*?)</h1></a></dt>'
        novel_author_regex = '<dd><span>作者：</span>(.*?)</dd>'
        novel_intro_regex = '<dd class="info">(.*?)</dd>'
        latest_chapter_info_regex = '<dd><span>最新：<a href="(.*?)" title=".*?" target="_blank">(.*?)</a></span></dd>'
        refresh_time_regex = '<dd><span>更新时间：(.*?)</span></dd>'
        novel_img_regex = '<a href=".*?"><img src="(.*?)" alt=".*?"></a>'
        
        novel_info = re.findall(novel_info_regex,html,re.S)
        novel_author = re.findall(novel_author_regex,html)
        novel_intro = re.findall(novel_intro_regex,html,re.S)
        latest_chapter_info = re.findall(latest_chapter_info_regex,html)
        refresh_time = re.findall(refresh_time_regex,html)
        novel_img = re.findall(novel_img_regex,html)

        novel_a = {}
        for i in range(len(novel_info)):
            novel_a[novel_info[i][1]] = {
                                            'novel_author':novel_author[i],
                                            'novel_author_url':None,
                                            'novel_url':novel_info[i][0],
                                            'novel_intro':novel_intro[i],
                                            'novel_type':None,
                                            'refresh_time':refresh_time[i],
                                            'novel_status':None,
                                            'novel_words':None,
                                            'novel_img':novel_img[i],
                                            'latest_chapter':latest_chapter_info[i][1],
                                            'latest_chapter_url':latest_chapter_info[i][0],
                                            }
        return novel_a


        



    


    def all_book_info(self,url):
        html = self.html(url)
        book_img_regex = '<img src="(.*?)" title=".*?" onerror=.*?'
        book_name_regex = '<h1>(.*?)</h1>'
        book_author_regex = '作者：(.*?)&nbsp;&nbsp;&nbsp;&nbsp;类别'
        book_type_regex = '类别：(.*?)<br>'
        book_intro_regex = '最新章节预览：(.*?)</div>'
        book_update_time_regex = '更新时间：(.*?)<br>'
        book_news_chapter_regex = '最新章节 ：<a href="(.*?)">(.*?)</a>'
        book_chapter_list_regex = '<li><a title=".*?" href="(.*?)">(.*?)</a></li>'


        

        book_img = re.findall(book_img_regex,html)[0]
        book_name = re.findall(book_name_regex,html,re.S)[0]
        book_author = re.findall(book_author_regex,html)[0]
        book_type = re.findall(book_type_regex,html)[0]
        book_intro = re.findall(book_intro_regex,html,re.S)[0]
        book_update_time = re.findall(book_update_time_regex,html)[0]
        book_news_chapter = re.findall(book_news_chapter_regex,html)[0]
        book_chapter_list = re.findall(book_chapter_list_regex,html)
        chapter_info_dict = {}
        for i in book_chapter_list:
            chapter_info_dict[i[1]] = url+i[0]

        novel_dict = {'novel_name':book_name,
                      'novel_author':book_author,
                      'novel_author_url':None,
                      'novel_intro':book_intro,
                      'novel_type':book_type,
                      'refresh_time':book_update_time,
                      'novel_status':None,
                      'novel_words':None,
                      'novel_img':book_img,
                      'latest_chapter':book_news_chapter[1],
                      'laster_chapter_url':url+book_news_chapter[0],
                      'novel_chapter_info':chapter_info_dict
                      }

        
        return novel_dict
            




   

    def chapter_content(self,url):
        html = self.html(url)
        url_1 = url.replace(url.split('/')[-1],'')
        chapter_content_regex = '<br /><br />(.*?)</div>'
        chapter_name_regex = '<h1>(.*?)</h1>'
        chapter_prev_regex = '<a id="previewpage" href="(.*?)" title=".*?">(.*?)</a>'
        chapter_next_regex = '<a id="nextpage" href="(.*?)"  title=".*?">(.*?)</a>'
        chapter_catalague_regex = '<a id="mulu" href="(.*?)" title=".*?">(.*?)</a>'
        book_name_regex = '><b>(.*?)</b></a>'
        
        chapter_content = re.findall(chapter_content_regex,html,re.S)[0]
        chapter_name = re.findall(chapter_name_regex,html)[0]
        chapter_catalague = re.findall(chapter_catalague_regex,html)[0]
        chapter_prev = re.findall(chapter_prev_regex,html)[0]
        chapter_next = re.findall(chapter_next_regex,html)[0]
        book_name = re.findall(book_name_regex,html)[0]

        chapter_info = {'chapter_name':chapter_name,
                        'chapter_content':chapter_content,
                        'novel_name':book_name,
                        'novel_author':None,
                        'novel_words':None,
                        'novel_catalog':url_1+chapter_catalague[0],
                        'pre_chapter':url_1+chapter_prev[0],
                        'next_chapter':url_1+chapter_next[0]
                        }

        

        return chapter_info



if __name__ == '__main__':
    a = Qksw().homepage()
    print(a)
    
    


    
    
    



